package com.wanlian.sql;

import java.util.List;

import com.wanlian.util.StringUtil;

public class SqlUtil {
	public static String getLikeCondition(String source) {
		String likevalue = "%" + source + "%";
		return likevalue;
	}

	public static String getTimeCondition(String title, String starttime, String endtime, List<Object> params) {
		String result = "";
		if (StringUtil.hasValue(starttime)) {
			result += String.format(" and %s>=?", title);
			params.add(starttime);
		}
		if (StringUtil.hasValue(endtime)) {
			result += String.format(" and %s <=?", title);
			java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
			ts = java.sql.Timestamp.valueOf(endtime + " 23:59:59");
			params.add(ts);
		}
		return result;
	}

	public static String getTimeCondition(STimeConverter timeConverter, List<Object> params) {
		String result = "";
		if (StringUtil.hasValue(timeConverter.getStartTime())) {
			result += String.format(" and %s>=?", timeConverter.getName());
			params.add(timeConverter.getStartTime());
		}
		if (StringUtil.hasValue(timeConverter.getEndTime())) {
			result += String.format(" and %s <=?", timeConverter.getName());
			java.sql.Timestamp ts = new java.sql.Timestamp(System.currentTimeMillis());
			ts = java.sql.Timestamp.valueOf(timeConverter.getEndTime() + " 23:59:59");
			params.add(ts);
		}
		return result;
	}
}
